package com.zzy.reflect;

import com.zzy.model.TempModel;

import java.io.Serializable;
import java.lang.reflect.Field;

public class UtilReflect<T> implements Serializable {
    private Class<T> clazz;
    public UtilReflect(Class<T> clazz){
        this.clazz = clazz;
    }

    private String compareModel(T oldModel,T newModel,boolean isCompareNull){
        String msg = "";
        try{

            Field[] allFields = clazz.getDeclaredFields();
            for(int i =0;i<allFields.length;i++){
                Field field = allFields[i];
                String prefix = field.getDeclaringClass().toString();
                String fieldName = field.getName().replace(prefix,"");
                field.setAccessible(true);
                Object oldVal = field.get(oldModel);
                Object newVal = field.get(newModel);
                if(newVal==null){
                    if(isCompareNull){
                        if(oldModel!=null && oldVal.toString().length()>0){//从有值变为空
                            msg += fieldName + "：" + oldVal + "-->" + newVal +"；";
                        }
                    }
                }else {
                    if(oldVal==null){
                        msg += fieldName + "：" + oldVal + "--> NULL ；";
                    }else{
                        if(!oldVal.toString().equals(newVal.toString())){//从有值变为空
                            msg += fieldName + "：" + oldVal + "-->" + newVal +"；";
                        }
                    }
                }
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return msg;
    }

    private String compareModelAnnotation(T oldModel,T newModel,boolean isCompareNull){
        String msg = "";
        try{

            Field[] allFields = clazz.getDeclaredFields();
            for(int i =0;i<allFields.length;i++){
                Field field = allFields[i];
                if(!field.isAnnotationPresent(CompareAttribute.class)){//只要带注解的
                    continue;
                }
                CompareAttribute attribute = field.getAnnotation(CompareAttribute.class);
                String fieldName = attribute.name();
                field.setAccessible(true);
                Object oldVal = field.get(oldModel);
                Object newVal = field.get(newModel);
                if(newVal==null){
                    if(isCompareNull){
                        if(oldModel!=null && oldVal.toString().length()>0){//从有值变为空
                            msg += fieldName + "：" + oldVal + "-->" + newVal +"；";
                        }
                    }
                }else {
                    if(oldVal==null){
                        msg += fieldName + "：" + oldVal + "--> NULL ；";
                    }else{
                        if(!oldVal.toString().equals(newVal.toString())){//从有值变为空
                            msg += fieldName + "：" + oldVal + "-->" + newVal +"；";
                        }
                    }
                }
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return msg;
    }

    public static void main(String[] args) {
        TempModel t1 = new TempModel();
        t1.setId("aaaa");
        t1.setAge(12);
        t1.setName("名字1");
        TempModel t2 = new TempModel();
        t2.setId("bb");
        t2.setAge(13);
        t2.setName("名字33");
        UtilReflect<TempModel> util = new UtilReflect<>(TempModel.class);
        String msg = util.compareModelAnnotation(t1,t2,true);
        System.out.println(msg);
    }

}
